package com.yudean.esb.cached;

import com.yudean.esb.dao.esb.EsbDataSourceDao;
import com.yudean.esb.domain.esb.EsbDataSource;
import com.yudean.esb.util.SpringUtil;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.TimerTask;

/**
 * Created by zenghui on 2015-05-21.
 * desc:
 */
public class DataSourceScanner extends TimerTask {
    private static final String TAG = "DataSourceScanner";
    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(DataSourceScanner.class);

    @Override
    public void run() {
        EsbDataSourceDao dao = (EsbDataSourceDao) SpringUtil.getBean("esbDataSourceDao");
        if (dao == null) return;
        // modify by zenghui get enable database connection 2015-06-03 begin
        List<EsbDataSource> list = dao.getAllEnable();
        // modify by zenghui end
        if (list == null) return;

        // add by zenghui clear database connection
        DataSourcesCollection.clearDataSourceMap();
        // add by zenghui end

        for (EsbDataSource dataSource : list) {
            //logger.info("loadDataSources():add DatasourceID==>{}", dataSource.getID());
            DataSourcesCollection.putDataSource(dataSource);
        }
    }
}
